package com.ziven.common.widget.window;

import android.view.Gravity;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;

import androidx.annotation.MenuRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.PopupMenu;

import com.ziven.easygo.util.Standard;

/**
 * @author Ziven
 */
public final class CommonMenu implements Standard {

    @NonNull
    public final PopupMenu popupMenu;

    @NonNull
    public final Menu menu;

    public CommonMenu(@NonNull View view, int styleId) {
        popupMenu = new PopupMenu(view.getContext(), view, Gravity.NO_GRAVITY, 0, styleId);
        menu = popupMenu.getMenu();
    }

    public CommonMenu listener(PopupMenu.OnMenuItemClickListener listener) {
        popupMenu.setOnMenuItemClickListener(listener);
        return this;
    }

    public CommonMenu listener(PopupMenu.OnDismissListener listener) {
        popupMenu.setOnDismissListener(listener);
        return this;
    }

    public CommonMenu setGravity(int gravity) {
        popupMenu.setGravity(gravity);
        return this;
    }

    public CommonMenu menuRes(@MenuRes int menuRes) {
        popupMenu.inflate(menuRes);
        return this;
    }

    public void show() {
        popupMenu.show();
    }

    public void dismiss() {
        popupMenu.dismiss();
    }

    public CommonMenu menu(CharSequence title) {
        menu.add(title);
        return this;
    }

    public CommonMenu menu(@StringRes int titleRes) {
        menu.add(titleRes);
        return this;
    }

    public CommonMenu menu(int itemId, CharSequence title) {
        return menu(0, itemId, 0, title);
    }

    public CommonMenu menu(int groupId, int itemId, int order, CharSequence title) {
        menu.add(groupId, itemId, order, title);
        return this;
    }

    public CommonMenu menu(int itemId, @StringRes int titleRes) {
        return menu(0, itemId, 0, titleRes);
    }

    public CommonMenu menu(int groupId, int itemId, int order, @StringRes int titleRes) {
        menu.add(groupId, itemId, order, titleRes);
        return this;
    }

    public SubMenu subMenu(CharSequence title) {
        return menu.addSubMenu(title);
    }

    public SubMenu subMenu(@StringRes int titleRes) {
        return menu.addSubMenu(titleRes);
    }

    public SubMenu subMenu(int itemId, CharSequence title) {
        return subMenu(0, itemId, 0, title);
    }

    public SubMenu subMenu(int groupId, int itemId, int order, CharSequence title) {
        return menu.addSubMenu(groupId, itemId, order, title);
    }

    public SubMenu subMenu(int itemId, @StringRes int titleRes) {
        return subMenu(0, itemId, 0, titleRes);
    }

    public SubMenu subMenu(int groupId, int itemId, int order, @StringRes int titleRes) {
        return menu.addSubMenu(groupId, itemId, order, titleRes);
    }
}
